package com.emenu;

import java.util.ArrayList;
import java.util.List;


import com.emenu.application.StaticObject;
import com.emenu.bean.PrintBusRetailDetail;
import com.emenu.bean.PubTable;
import com.emenu.common.Common;
import com.emenu.common.GetWebServiceData;
import com.emenu.common.JsonUntil;
import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.AdapterView.OnItemClickListener;

public class FunCheckStatusFragment extends Fragment {
	
	public EditText fun_statab;
	public TextView fun_statabname;
	public ListView stalistView;
//	public ProgressBar staprogressBar;
	
	public Button allcall;
	public Button siglecall;
	public Button allhurry;
	
	public Button allcallcancle;
	public Button siglecallcancle;
	public Button allhurrycancle;
	
	public Button siglehurry;
	public Button hotcall;
	public Button coldcall;
	
	public Button siglehurrycancle;
	public Button hotcallcancle;
	public Button coldcallcancle;
	
	public StaticObject SO;
	Context cn;
	public static final String SET_NAME = "jpSetFile";
	private String url;
	//对应表Print_bus_retail_detail
	List<PrintBusRetailDetail> list = new ArrayList<PrintBusRetailDetail>();
	
	@Override
	public void onAttach(Activity activity) {
		super.onAttach(activity);
		SO = (StaticObject) activity.getApplication();		
		cn = this.getActivity();		
	}
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {

		View v = inflater.inflate(R.layout.funcheckstatus, container, false);

		findViewsById(v);
		setListener();
		
		SharedPreferences settings = cn.getSharedPreferences(SET_NAME, 0);
		url = settings.getString("url", "");

		return v;
	}
	
	/** 视图 样式 */
	private void findViewsById(View v) {
		fun_statab = (EditText) v.findViewById((R.id.fun_statab));
		fun_statabname = (TextView) v.findViewById((R.id.fun_statabname));
		stalistView = (ListView) v.findViewById(R.id.stalistView);
		
		allcall = (Button) v.findViewById((R.id.allcall));
		siglecall = (Button) v.findViewById((R.id.siglecall));
		allhurry = (Button) v.findViewById((R.id.allhurry));
		
		allcallcancle = (Button) v.findViewById((R.id.allcallcancle));
		siglecallcancle = (Button) v.findViewById((R.id.siglecallcancle));
		allhurrycancle = (Button) v.findViewById((R.id.allhurrycancle));
		
		siglehurry = (Button) v.findViewById((R.id.siglehurry));
		hotcall = (Button) v.findViewById((R.id.hotcall));
		coldcall = (Button) v.findViewById((R.id.coldcall));
		
		siglehurrycancle = (Button) v.findViewById((R.id.siglehurrycancle));
		hotcallcancle = (Button) v.findViewById((R.id.hotcallcancle));
		coldcallcancle = (Button) v.findViewById((R.id.coldcallcancle));
		

//		staprogressBar = (ProgressBar) v.findViewById(R.id.staprogressBar);	 
//		staprogressBar.setIndeterminate(false);

	}
	
	/** 设置监听器 */
	private void setListener() {
		
		fun_statab.addTextChangedListener(txcswatcher);
		stalistView.setOnItemClickListener(stalistViewlistener);
		
		allcall.setOnClickListener(allcallListener);
		siglecall.setOnClickListener(siglecallListener);
		allhurry.setOnClickListener(allhurryListener);
		
		allcallcancle.setOnClickListener(allcallcancleListener);
		siglecallcancle.setOnClickListener(siglecallcancleListener);
		allhurrycancle.setOnClickListener(allhurrycancleListener);
		
		siglehurry.setOnClickListener(siglehurryListener);
		hotcall.setOnClickListener(hotcallListener);
		coldcall.setOnClickListener(coldcallListener);
		
		siglehurrycancle.setOnClickListener(siglehurrycancleListener);
		hotcallcancle.setOnClickListener(hotcallcancleListener);
		coldcallcancle.setOnClickListener(coldcallcancleListener);
	}
	
	public TextWatcher txcswatcher = new TextWatcher() {
		@Override
		public void beforeTextChanged(CharSequence s, int start, int count,
				int after) {
			// TODO Auto-generated method stub

		}

		@Override
		public void afterTextChanged(Editable s) {

//			staprogressBar.setVisibility(View.VISIBLE);

			for (PubTable pt : SO.getPubTableList()) {
				if ((fun_statab.getText() + "").equals(pt.getHelpcode())) {
					fun_statabname.setText(pt.getName());
					// 台号异步取对账单表（Print_bus_retail_detail）数据
					new getPBRDTask().execute(pt.getCode());
					stalistViewAdapter adapter = new stalistViewAdapter();
					stalistView.setAdapter(adapter);
				}

			}
//			staprogressBar.setVisibility(View.GONE);

		}

		@Override
		public void onTextChanged(CharSequence s, int start, int before,
				int count) {

		}

	};
	
	public class stalistViewAdapter extends BaseAdapter {

		@Override
		public int getCount() {

			return list.size();
		}

		@Override
		public Object getItem(int position) {

			return list.get(position);
		}

		@Override
		public long getItemId(int position) {

			return position;

		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			if (convertView == null) {
				convertView = ((FunMainActivity) cn).getLayoutInflater()
						.inflate(R.layout.funckstatuslist, null);
			}

			TextView ckstamenu_code;
			TextView ckstamenu_name;
			TextView ckstatagname;
			TextView ckstReminderfood;
			TextView ckstCalledSingle;
			
			ckstamenu_code = (TextView) convertView.findViewById(R.id.ckstamenu_code);
			ckstamenu_name = (TextView) convertView.findViewById(R.id.ckstamenu_name);
			ckstatagname = (TextView) convertView
					.findViewById(R.id.ckstatagname);
			ckstReminderfood = (TextView) convertView
					.findViewById(R.id.ckstReminderfood);
			ckstCalledSingle = (TextView) convertView
			.findViewById(R.id.ckstCalledSingle);

			ckstamenu_code.setText(list.get(position).getMenu_code());
			ckstamenu_name.setText(list.get(position).getMenu_name());

			ckstatagname.setText(list.get(position).getTagname());
			ckstReminderfood.setText(list.get(position).getReminderfoodName());
			ckstCalledSingle.setText(list.get(position).getCalledSingleName());

			return convertView;
		}

	}

	/**
	 * 异步得到PrintBusRetailDetail
	 */
	public class getPBRDTask extends AsyncTask<String, Void, Boolean> {

		// 执行onPreExecute()完后立即调用
		@Override
		protected Boolean doInBackground(String... params) {

			try {
				// 参数下载
				String masjson = GetWebServiceData
						.WSPrintBusRetailDetailJson(url,"100000","1") ;
				
				list = JsonUntil.getPrintBusRetailDetailList(masjson);
				return true;
			} catch (Exception e) {
				e.printStackTrace();

			}
			return false;
		}

		// doInBackground后执行
		// result 的得自于doInBackgroun
		@Override
		protected void onPostExecute(Boolean result) {

			if (!result) {
				Common.alertFinish(cn, "数据下载失败");
			}
		}

	}
	
	private OnItemClickListener stalistViewlistener = new OnItemClickListener() {

		@Override
		public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
				long arg3) {
			// 选中的记录的编号
			String mascode = list.get(arg2).getRetail_code();
			 
		}

	};
	
	//整单叫单 把整划单单据号的PrintBusRetailDetail的CalledSingle 置为1
	private OnClickListener allcallListener = new OnClickListener() {

		@Override
		public void onClick(View v) {

		}
		 
	};
	//单品叫单 把选择划单某条菜品的CalledSingle 置为1
	private OnClickListener siglecallListener = new OnClickListener() {

		@Override
		public void onClick(View v) {

		}
		 
	};
	//整单催单 把整划单单据号的PrintBusRetailDetail的Reminderfood 置为1   
	private OnClickListener allhurryListener = new OnClickListener() {

		@Override
		public void onClick(View v) {

		}
		 
	};
	//取消整单叫单
	private OnClickListener allcallcancleListener = new OnClickListener() {

		@Override
		public void onClick(View v) {

		}
		 
	};
	//取消单品叫单
	private OnClickListener siglecallcancleListener = new OnClickListener() {

		@Override
		public void onClick(View v) {

		}
		 
	};
	//取消整单催单 
	private OnClickListener allhurrycancleListener = new OnClickListener() {

		@Override
		public void onClick(View v) {

		}
		 
	};
	//单品催单 把选择划单某条菜品的Reminderfood 置为1
	private OnClickListener siglehurryListener = new OnClickListener() {

		@Override
		public void onClick(View v) {

		}
		 
	};
	//热菜叫单 Rljtype=0表示热菜
	private OnClickListener hotcallListener = new OnClickListener() {

		@Override
		public void onClick(View v) {

		}
		 
	};
	//冷菜叫单 Rljtype=1表示冷菜
	private OnClickListener coldcallListener = new OnClickListener() {

		@Override
		public void onClick(View v) {

		}
		 
	};
	//取消单品催单
	private OnClickListener siglehurrycancleListener = new OnClickListener() {

		@Override
		public void onClick(View v) {

		}
		 
	};
	//取消热菜叫单
	private OnClickListener hotcallcancleListener = new OnClickListener() {

		@Override
		public void onClick(View v) {

		}
		 
	};
	//取消冷菜叫单
	private OnClickListener coldcallcancleListener = new OnClickListener() {

		@Override
		public void onClick(View v) {

		}
		 
	};
}
